﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using OfficeSVN;

namespace WordSVN {

    public partial class ThisAddIn: IOfficePlugIn {

        public static Word.Application WordApplication;
        public static OfficeSvnRibbon WordRibbon;
        private static SubversionController _controller;

        private void ThisAddIn_Startup(object sender, System.EventArgs e) {

            WordApplication = this.Application;
            WordApplication.DocumentChange += Application_DocumentChange;
            WordApplication.DocumentOpen += Application_DocumentOpen;
        }

        public override void BeginInit()
        {
            _controller = new SubversionController(this);

            base.BeginInit();
        }

        protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject() {
            
            if (_controller != null)
                return _controller.GetRibbon();

            return null;
        }

        void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document doc) {
            OnDocumentChangedEvent(doc.FullName);
        }

        void Application_DocumentChange() {
            OnDocumentChangedEvent(WordApplication.ActiveDocument.FullName);
        }

        private void OnDocumentChangedEvent(string document) {
            // Copy to a temporary variable to be thread-safe.
            EventHandler<DocumentEventArgs> temp = DocumentChanged;
            if (temp != null)
                temp(null, new DocumentEventArgs(document));
        }

        void WordRibbon_NeedDocumentEvent(object sender, DocumentEventArgs e) {
            e.Document = WordApplication.ActiveDocument.FullName;
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup() {
            this.Startup += ThisAddIn_Startup;
        }

        public event EventHandler<DocumentEventArgs> DocumentChanged;

        public void OpenDocument(string document)
        {
            Object missingObject = System.Reflection.Missing.Value;
            object documentObject = document;

            WordApplication.Documents.Open(ref documentObject, ref missingObject, ref missingObject, ref missingObject, ref missingObject, ref missingObject, ref missingObject, ref missingObject, ref missingObject, ref missingObject, ref missingObject, ref missingObject, ref missingObject, ref missingObject, ref missingObject, ref missingObject);
        }

        public void CloseDocument(string document)
        {
            Object missingObject = System.Reflection.Missing.Value;
            WordApplication.ActiveDocument.Close(ref missingObject, ref missingObject, ref missingObject);
        }

        public void SaveDocument(string document)
        {
            WordApplication.ActiveDocument.Save();
        }

        public void ReloadDocument(string document)
        {
            // Word does not support a reload of the current document (afaik). 

            // So close the document ...
            CloseDocument(document);

            // ... and open it again.
            OpenDocument(document);
        }
    }
}
